import { loginApi } from "@/api/user/user";
import { LoginParam } from "@/api/user/userModel";
import { useRouter } from "vue-router";
import { getCurrentInstance } from "vue";


// 引入的是我们自己的store
import { useStore } from "@/store";

export default function useLogin(formData:LoginParam) {
    const { proxy } = getCurrentInstance() as any
    const store = useStore()
    const router = useRouter()

    // 登录提交
    const handleLogin = async () => {
        // 表单验证
        // console.log('proxy===', proxy)
        proxy.$refs.loginFormRef.validate(async(valid:boolean) => {
            // console.log(valid)
            if(valid) {
                // await loginApi(formData).then(res => {
                //     console.log(res)
                // })
                store.dispatch('user/login',formData).then(res => {
                    console.log('登录成功')
                    // 如果把store里的user里的return new Promise<Result>  Result去掉就不会自动提示res.data/res.code
                    // store.dispatch('user/getInfo')
                    // if(res) {
                    //     router.push({path: '/'})
                    // }
                    router.push({path: '/'})
                })
            } else {

            }
        })
    }

    return {
        handleLogin
    }
}